package com.tenbent.product.center.insures.service.impl;

import com.tenbent.product.base.service.impl.BaseServiceImpl;
import com.tenbent.product.center.insures.bo.Product;
import com.tenbent.product.center.insures.dto.ProductDto;
import com.tenbent.product.center.insures.dto.SafeguardCategoryTypeDto;
import com.tenbent.product.center.insures.dto.SafeguardPackageDto;
import com.tenbent.product.center.insures.dto.SkuInfoDto;
import com.tenbent.product.center.insures.mapper.ProductMapper;
import com.tenbent.product.center.insures.service.ProductService;
import com.tenbent.product.center.insures.service.SafeguardCategoryTypeService;
import com.tenbent.product.center.insures.service.SafeguardPackageService;
import com.tenbent.product.center.insures.service.SkuInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 产品服务层
 *
 * @author Randy
 *
 *         Created by ThinkPad on 2017/8/21.
 */
@Service(value = "productServiceImpl")
public class ProductServiceImpl extends BaseServiceImpl<Product> implements ProductService {

	public ProductServiceImpl() {
		this.setClazz(Product.class);
	}

	@Autowired
	private ProductMapper productMapper;

	@Autowired
	@Qualifier("safeguardCategoryTypeServiceImpl")
	private SafeguardCategoryTypeService safeguardCategoryTypeService;

	@Autowired
	@Qualifier("skuInfoServiceImpl")
	private SkuInfoService skuInfoService;

	@Autowired
	@Qualifier("safeguardPackageServiceImpl")
	private SafeguardPackageService safeguardPackageService;

	@Override
	public ProductDto findProductByCode(String pdCode) {

		Product query = new Product();
		query.setPdCode(pdCode);
		List<Product> products = this.queryObj(query);
		if (products == null || products.isEmpty()) {
			return null;
		}
		Product product = products.get(0);
		ProductDto productDto = new ProductDto(product);
		// 装配保障项目大类
		List<SafeguardCategoryTypeDto> safeguardCategoryTypeDtos = safeguardCategoryTypeService
				.findSafeguardCategoryTypeByProductId(productDto.getId());
		productDto.setSafeguardCategoryTypes(safeguardCategoryTypeDtos);
		// 装配套餐
		List<SafeguardPackageDto> safeguardPackageDtos = safeguardPackageService
				.findSafeguardPackageByProductId(productDto.getId());
		productDto.setSafeguardPackages(safeguardPackageDtos);
		// 装配sku信息
		List<SkuInfoDto> skuInfoDtos = skuInfoService.findSkuInfoByProductId(productDto.getId());
		productDto.setSkuInfos(skuInfoDtos);
		return productDto;
	}
}
